<template>
    <el-dialog v-model="visible"  title="内容页面" @close="cancel" width="35%">
    
    
        <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px">
          <!-- <el-form-item label="图片" prop="urlz">
            <el-upload
          v-model:file-list="dataForm.urls"
					:action="constant.uploadUrl"
					:headers="{ Authorization: cache.getToken() }"
					:before-upload="beforeUpload"
					:on-success="handleSuccess"
					:show-file-list="false"
          accept="image/*"
				>
					<el-button v-auth="'sys:attachment:save'" icon="Plus" type="primary">上传</el-button>
				</el-upload>
        </el-form-item>

     
      <el-image
         v-if="adxiuurl"
          style="width: 100px; height: 100px;margin-left: 7rem"
          :src="adxiuurl"
          :zoom-rate="1.2"
          :max-scale="7"
          :min-scale="0.2"
          /> -->
        
          <el-form-item label="标题" prop="title">
          <el-input v-model="dataForm.title"    
          maxlength="20"
          show-word-limit
          placeholder="请输入内容，最多输入20个字符" ></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
            type="textarea"
            :rows="2"
             maxlength="200"
             show-word-limit
            placeholder="请输入内容，最多输入200个字符"
            v-model="dataForm.content">
          </el-input>

          <!-- <el-input v-model="dataForm.content"></el-input> -->
        </el-form-item>

    
  
      </el-form>
      <template #footer>
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="submit()">确定</el-button>
      </template>
    </el-dialog>
 

  </template>
  
  <script setup lang="ts">
  import cache from '@/utils/cache'
  import { reactive, ref } from 'vue'
  import { ElMessage } from 'element-plus/es'
  import { useCrud } from '@/hooks'
  import { Delete, Download, Plus, ZoomIn } from '@element-plus/icons-vue'
  import constant from '@/utils/constant'
  import type { UploadFile } from 'element-plus'
  import { addPColumn, addSColumn, editPColumn, editSColumn ,CarouselNewdedit,sanchuan} from '@/api/allApi'
  import { v } from 'vxe-table'
  import type { UploadProps, UploadUserFile } from 'element-plus'
import { el } from 'element-plus/es/locale'
  const emit = defineEmits(['resetList'])
  
  const visible = ref(false)

  const dialogType = ref('addP')
  const dataFormRef = ref()
  const dataForm = reactive({
    // icons: "",
    id: '',
    name: "",
    urls: "",
    size: 0,
    platform: "",
    tenantId: 0,
    type: "",
    websiteId: 0,
    creator: 0,
    createTime: "",
    title: "",
    content: "",
    updateTime: "",
    updator: 0
  })



  const handleSuccess: UploadProps['onSuccess'] = (res, file) => {
	if (res.code !== 0) {
		ElMessage.error('上传失败：' + res.msg)
		return false
	}

	Object.assign(dataForm, res.data)

}

const beforeUpload: UploadProps['beforeUpload'] = file => {
	if (file.size / 1024 / 1024 / 100 > 1) {
		ElMessage.error('文件大小不能超过100M')
		return false
	}
	return true
}

const adxiuurl = ref("")

const adxiuCodes  = ref("")

// 回显数据
        const init = (type: string, row: any) => {
            
            console.log(row,'ziyemian')
     
              // // 检查row是否存在
                if (row && row.url) {
                  console.log(row,"rrrrrrrrr");
                  
                    const xiuurl = row.url;
                           // 真数据
                    adxiuurl.value = xiuurl
                 } else {
                    console.error('row is undefined or row.icon is missing');
                }
                    Object.assign(dataForm, row);
                        // 显示对话框
                        visible.value = true;

              }


                  const id = ref('')
                  const name = ref('')
                  const url = ref('')
                  const sizeas = ref('')  
                  const platform = ref('')
                  const tenantId = ref('')
                  const websiteId  = ref("")
                  const title  = ref("")
                  const content  = ref("")
                  const typess  = ref("")

                  const creator  = ref("")
                  const createTime  = ref("")
                  const updateTime  = ref("")
                  const updator  = ref("")



// 新增数据  修改数据

        const dataRules = reactive({})
        const rowDate = reactive({})
        
        let lastSubmitTime = 0; // 上一次提交的时间戳
        const SUBMIT_COOLDOWN_TIME = 3 * 60 * 1000; // 3分钟的毫秒数

        const submit = () => {
            const currentTime = Date.now(); // 当前时间戳

            // // 检查是否在冷却时间内
            if (currentTime - lastSubmitTime < SUBMIT_COOLDOWN_TIME) {
                ElMessage.warning('请在3分钟后再试一次');
                return;
            }
             dataForm.tenantId= JSON.parse(localStorage.getItem('userInfo')).id, //租户id
            dataForm.websiteId= Number(localStorage.getItem('WebsiteId')), //网站id

            name.value = dataForm.name
            url.value = dataForm.url; 
            sizeas.value = dataForm.size
            platform.value = dataForm.platform
            tenantId.value = dataForm.tenantId
            websiteId.value = dataForm.websiteId
            title.value = dataForm.title
            content.value = dataForm.content
            typess.value = dataForm.type
            id.value = dataForm.id
            creator.value = dataForm.creator
            createTime.value = dataForm.createTime
            updateTime.value = dataForm.updateTime
            updator.value = dataForm.updator

            if (dataForm.id === '') {
                console.log(dataForm,"id为空,新增");

                CarouselNewdedit({
                     name:name.value ,
                     url:url.value ,
                     size:sizeas.value ,
                     platform:platform.value ,
                     tenantId:tenantId.value ,
                     type:typess.value ,
                     websiteId:websiteId.value ,
                     title:title.value ,
                     content:content.value ,
                  
                }).then((res: any) => {
                    console.log(res, 'res');
                    ElMessage.success('添加操作成功');
                    lastSubmitTime = currentTime; // 更新提交时间
                    setTimeout(function() {
                        location.reload();
                    }, 500); // 
    
                    visible.value = false;
                }).catch((err: any) => {
                    ElMessage.error(err.message);
                    console.log("报错");
                });
            } else {
                console.log("id不为空,修改");
                CarouselNewdedit({
                     name:name.value ,
                     url:url.value ,
                     size:sizeas.value ,
                     platform:platform.value ,
                     tenantId:tenantId.value ,
                     type:typess.value ,
                     websiteId:websiteId.value ,
                     title:title.value ,
                     content:content.value ,
                     id:id.value ,
                     creator:creator.value ,
                     createTime:createTime.value ,
                     updateTime:updateTime.value ,
                     updator:updator.value ,
                }).then((res: any) => {
                    console.log(res, 'res');
                    lastSubmitTime = currentTime; // 更新提交时间
                    setTimeout(function() {
                        location.reload();
                    }, 1000); 
                    ElMessage.success('修改操作成功');
                    visible.value = false;
                    emit('resetList');
                }).catch((err: any) => {
                    ElMessage.error(err.message);
                });
            }
        }
  

  const cancel = () => {
         dataForm.name = '';
       dataForm.url = ' ';
             dataForm.size = ' ';
                 dataForm.platform = '';
                 dataForm.tenantId = '';
                   dataForm.websiteId = '';
           dataForm.title = '';
               dataForm.content = '';
             dataForm.type = '';
     dataForm.id = '';
               dataForm.creator = '';
                     dataForm.createTime =' ';
                     dataForm.updateTime =' ';
               dataForm.updator =''

      // name.value = ''
      // url.value = ''
      // sizeas.value = ''
      // platform.value = ''
      // tenantId.value = ''
      // typess.value = ''
      // websiteId.value = ''
      // title.value = ''
      // content.value = ''
      // id.value = ''
      // creator.value = ''
      // createTime.value = ''
      // updateTime.value = ''
      // updator.value = ''
      visible.value = false
      // dataForm.id = '';
      // dataForm.name = '';
      // dataForm.updateTime = '';
      adxiuurl.value=''
      adxiuCodes.value = ''
      dataFormRef.value.resetFields()
  }
  
  defineExpose({
      init
  })
  </script>
  
  <style lang="scss" scoped>
.custom-label-width .el-form-item__label {
  width: 120px; /* 或者其他你需要的宽度 */
}


</style>
  